import 'dart:developer';

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/utils/widget_utils.dart';

import '../../entity/other/resource_entity.dart';
import '../../server/api/other_api.dart';
import '../base/qkd_text.dart';

enum ImageShareType{
  viewpoint(name: "viewpoint",backHeight: 89,text: "专家方案",),
  expert(name: "expert",backHeight: 124,text: "专家方案",);

  final String name;
  final double backHeight;
  final String text;

  const ImageShareType({required this.name,required this.backHeight,required this.text});
}

class ShareImage extends StatefulWidget{
  const ShareImage({super.key, required this.widgets, required this.type, required this.param});
  const ShareImage.viewpoint({super.key, required this.widgets, required this.param}):type = ImageShareType.viewpoint;
  const ShareImage.expert({super.key, required this.widgets, required this.param}):type = ImageShareType.expert;

  final List<Widget> widgets;
  final ImageShareType type;
  final String param;

  @override
  State<ShareImage> createState() => _ShareImageState();
}

class _ShareImageState extends State<ShareImage> {
  String url = "";
  
  Future getUrl() async{
    List<ResourceEntity>? urlInfo = await OtherApi.getAppList("app_page_share");
    String? rootUrl = urlInfo?.firstWhere((element) => element.button == "root").content;
    String? channel = urlInfo?.firstWhere((element) => element.button == "channel").content;
    if(rootUrl != null){
        url = rootUrl;
    }
    url = "$url${widget.param}${channel.hasValue?"&qd=$channel":""}";
    update();
  }

  @override
  void initState() {
    getUrl();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return AbsorbPointer(
      child: Container(
        color: Colours.greyF2F3F7,
        child: Stack(
          children: [
            Image.asset(Utils.getImgPath("share_${widget.type.name}.png"),width: Get.width),
            Column(
              children: [
                Container(height: Get.width*widget.type.backHeight/375),
                ...widget.widgets,
                Row(
                  children: [
                    Image.asset(Utils.getImgPath("qxb_logo.png"),width: 44),
                    Container(width: 8),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          QkdText("扫码查看${widget.type.text}",
                            fontSize: 16,
                            fontWeight: FontWeight.w500,
                          ),
                          QkdText("下载球客岛，看更多比赛数据",
                            maxLines: 2,
                            fontSize: 12,
                            color: Colours.grey66
                          )
                        ],
                      ),
                    ),
                    Container(
                      clipBehavior: Clip.antiAlias,
                      decoration: BoxDecoration(
                        color: Colours.white,
                        borderRadius: BorderRadius.circular(3)
                      ),
                      child: Container(
                        height: 80,
                        width: 80,
                        margin: const EdgeInsets.all(4),
                        child: QrImageView(
                          data: url,
                          size: 80,
                          version: QrVersions.auto,
                          // gapless: false,
                          errorCorrectionLevel: QrErrorCorrectLevel.H,
                          padding: EdgeInsets.zero,
                          backgroundColor: Colours.white,
                          embeddedImage: AssetImage(Utils.getImgPath("qxb_logo.png")),
                        ),
                      ),
                    )
                  ]
                ).paddingAll(16)
              ],
            )
          ],
        ),
      ),
    );
  }
}